/*************************************************************************
 * File Name:    Remove_Duplicates_from_Sorted_List.cc
 * Author:       zero91
 * Mail:         jianzhang9102@gmail.com
 * Created Time: 2013-11-6 10:47:49
 * 
 * Description:  
 |------------------------------------------------------------------------
 |Problem: Remove Duplicates from Sorted List
 |
 | Given a sorted linked list, delete all duplicates such that each element appear only once.
 |
 | For example,
 | Given 1->1->2, return 1->2.
 | Given 1->1->2->3->3, return 1->2->3.
 |------------------------------------------------------------------------
 ************************************************************************/

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <map>
#include <set>
#include <functional>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>

using namespace std;

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *deleteDuplicates(ListNode *head)
    {
        if (head == NULL) return NULL;
        
        ListNode *ans;
        ListNode *pre = head;
        
        ans = head;
        for (ListNode *t = head->next; t != NULL; t = t->next) {
            if (t->val == pre->val) {
                pre->next = t->next;
            } else {
                pre = t;
            }
        }
        return ans;
    }
};

